home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp48hor1 / facnum.src < prev    next >
Text File  |  1990-11-10  |  2KB  |  49 lines

  1. %%HP: T(3)A(R)F(.);
  2. @ by Mark Adler
  3. @ FACNUM (BYTES = 277.5, #74FFh)
  4. @ Given an integer, return its prime factorization as a list.
  5. @ For example, 16353 FACNUM returns { 3 3 23 79 }.
  6. \<<
  7.   @ initialize factor list
  8.   { } SWAP
  9.  
  10.   @ For this part of the program the stack is: n factorlist, where the two are
  11.   @ kept so that the product of the list times n is the original integer.
  12.  
  13.   @ factor out 2's
  14.   WHILE DUP 2 MOD NOT REPEAT
  15.     2 / SWAP 2 + SWAP END
  16.  
  17.   @ factor out 3's
  18.   WHILE DUP 3 MOD NOT REPEAT
  19.     3 / SWAP 3 + SWAP END
  20.  
  21.   @ start factor search at 5
  22.   5
  23.  
  24.   @ The stack is now: k n factorlist, where the second two are maintained as
  25.   @ before, and k is the largest 5 mod 6 integer less than or equal to the
  26.   @ last factor found (execpt initially when it is set to 5).
  27.  
  28.   @ search from k to sqrt(n) for factors---k must be 5 mod 6
  29.   WHILE OVER 1 \=/ REPEAT  @ do while n is not one
  30.     OVER \v/ FLOOR         @ go up to the floor of the square root
  31.     IFERR             @ (divide by zero used as a loop breaker)
  32.       FOR i           @ look at factors that are 1 and 5 mod 6
  33.   IF DUP i MOD NOT THEN
  34.     i 0 / END         @ if 5 mod 6 divides n, then cause error
  35.   IF DUP i 2 + MOD NOT THEN
  36.     i 2 + 0 / END          @ if 1 mod 6 divides n, then cause error
  37.       6 STEP
  38.     THEN DROP              @ got an error---trash the zero
  39.     ELSE DUP               @ end of loop---n divides n
  40.     END                    @ after this, stack is: factor n factorlist
  41.     ROT OVER + ROT ROT          @ add factor to list (factor n factorlist')
  42.     SWAP OVER / SWAP       @ divide out divisor (factor n' factorlist')
  43.     DUP 6 MOD 5 - 2 / +         @ set k' to largest 5 mod 6 <= divisor
  44.   END                 @ find next factor (k' n' factorlist')
  45.  
  46.   @ return list, dropping n and k
  47.   DROP2
  48. \>>
  49.